/*
 * *
 * author:cgm
 * date:18-5-21 下午1:29
 * /
 */

package com.kaili.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.ValidList;
import com.kaili.db.service.IHsOptRewbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.rewb.RewbAddDTO;
import com.kaili.pojo.dto.rewb.RewbDTO;
import com.kaili.pojo.vo.RewbQueryVo;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * package name:com.kaili.controller
 * author:hasee
 * date :2018/5/21
 * project:micro-service
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "回单服务", description = "回单服务API")
public class RewbController {
    @Autowired
    IHsOptRewbService rewbService;
    @Autowired
    private MessageSource messageSource;

    @GetMapping("/rewbs")
    @ApiOperation(value = "分页查询回单数据")
    Page<RewbQueryVo> getList(@ModelAttribute RewbDTO rewbDTO) {
        if (rewbDTO.getCur() != null && rewbDTO.getPageSize() != null) {
            Map<Object, Object> param = new HashMap<>();
            param.putAll(new BeanMap(rewbDTO));
            Integer siteId = LoginDetailUtils.getSiteId();
            if (siteId != null && siteId != 1){
                param.put("siteId", siteId);
            }
            Page<RewbQueryVo> pager = new Page<RewbQueryVo>(rewbDTO.getCur(), rewbDTO.getPageSize());
            pager = rewbService.getRewbList(pager, param);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.REWB_QUERY_ERR);
    }

    @PostMapping("/rewbsSend/bat")
    @ApiOperation(value = "回单寄送BS端")
    ResponseStatus doSendConfirm(@Validated @RequestBody ValidList<RewbAddDTO> rewbAddDTO) {
        rewbService.doUpdateSendConfirm(rewbAddDTO);
        return ResponseStatus.RESPONSE_OK;
    }

    @PutMapping("/rewbsCheck/bat")
    @ApiOperation(value = "回单验收BS端")
    ResponseStatus doCheckConfirm(@Validated @RequestBody ValidList<RewbAddDTO> rewbAddDTO) {
        rewbService.doUpdateCheckConfirm(rewbAddDTO);
        return ResponseStatus.RESPONSE_OK;
    }

    @PutMapping("/rewbsReturn/bat")
    @ApiOperation(value = "回单返厂BS端")
    ResponseStatus doReturnConfirm(@Validated @RequestBody ValidList<RewbAddDTO> rewbAddDTO) {
        rewbService.doUpdateReturnConfirm(rewbAddDTO);
        return ResponseStatus.RESPONSE_OK;
    }

}
